package com.yft.dao.base;

import com.yft.entity.base.BaseEntityUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Service("baseUtilsDao")
public class BaseDaoUtils {
	@Autowired(required = true)
	@Qualifier(BaseConstant.DAO_NAME)
	private BaseDao dao;

    public List getListBySql(final String queryString, final Object object, final int startPoint, final int endPoint){
        List list = dao.getHibernateTemplate().executeFind(new HibernateCallback() {
            public Object doInHibernate(Session session) {
                List list=new ArrayList();
                try {
                    Query query = session.createSQLQuery(queryString);
                    if (object!=null) query.setProperties(object);
                    if (startPoint>=0) query.setFirstResult(startPoint);
                    if (endPoint>0) query.setMaxResults(endPoint);
                    list= query.list();
                    session.flush();
                    session.close();
                    return list;
                } catch (Exception e) {
                    session.flush();
                    session.close();
                    return list;
                }
            }
        });
        return list;
    }
	@SuppressWarnings("unchecked")
	public List selectPropertis(String queryString,Object[] values) {
		if(values==null)
			return dao.getHibernateTemplate().find(queryString);
		else
			return dao.getHibernateTemplate().find(queryString, values);
	}
	public int updateSqlMethodOne(final String sql){
        Object result = dao.getHibernateTemplate().execute(new HibernateCallback() {
            @Override
            public Object doInHibernate(Session session)throws HibernateException, SQLException {
                SQLQuery sqlQuery = session.createSQLQuery(sql);
                return sqlQuery.executeUpdate();
            }

        });
        return (Integer)result;
    }

	public int updateSqlMethodTwo(String sql){
		Query query=dao.getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(sql);
		return query.executeUpdate();
	}
	
	public int updateHql(String sql){
		return (Integer)dao.executeHql(sql);
	}
	
	public int updateHql(String sql,Object condition){
		return (Integer)dao.executeHqlWithProperties(sql, condition);
	}

    public boolean updateHqlHasWhere(Object obj,String str,String whereStr){
        String hql = BaseEntityUtils.makeValues(obj, str, whereStr);
        if(hql==null) return false;
        Integer result =this.updateHql(hql,obj);
        return result!=null && result.intValue()>0 ? true : false;
    }

    public boolean saveAllEntity(List objects) {
        return dao.saveAllEntity(objects);
    }

}
